﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using EzDesk.DevHelper.DbMgr.Presenters;

namespace EzDesk.DevHelper.DbMgr.Views
{
    public partial class MainForm : Form, IMainFormView, IAppFunctionView
    {
        private DbMainPresenter _presenter;
        private AppFunctionPresenter _appFunctionPresention;
        private DataProcessPanel _dataProcessPanel = new DataProcessPanel();

        public MainForm()
        {
            InitializeComponent();
            feedPanel.Controls.Add(new FeedPanel());
            dataProcessPanel.Controls.Add(_dataProcessPanel);
            dataProcessPanel.Enabled = false;

            _presenter = new DbMainPresenter(this);
            _appFunctionPresention = new AppFunctionPresenter(this);
        }


        private void btnTestConn_Click(object sender, EventArgs e)
        {
            var targetServerName = txtServerName.Text;
            var instanceName = txtInstanceName.Text;

            var result = _presenter.ConnectToServer(targetServerName, instanceName);

            bool schemaAvailable = pnlOpr.Enabled = result.IsServerAvailable;
            bool dataAvailable = pnlData.Enabled = result.IsServerAvailable && targetServerName == ".";
            bool dataProcessAvailable = dataProcessPanel.Enabled = result.IsServerAvailable;

            if (schemaAvailable)
            {
                _presenter.FindDatabaseSchema();
            }

            if (dataAvailable)
            {
                _presenter.RefreshCurrentDatabases();
                _presenter.RefreshBackups();
            }

            lblServerStatus.Text = "已连接到服务器：" + result.ServerName;
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            _presenter.FindDatabaseSchema();
        }

        public void WriteLine(string str)
        {
            txtOutput.AppendText(str + Environment.NewLine + Environment.NewLine);
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            _presenter.RefreshDataSchema();
        }


        public void RefreshSchemaList(List<string> schemaList)
        {
            lstSchema.DataSource = schemaList;
            for (int i = 0; i < lstSchema.Items.Count; i++)
            {
                lstSchema.SetSelected(i, true);
            }
        }

        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            txtOutput.Clear();
        }

        private void btnUpdateAll_Click(object sender, EventArgs e)
        {
            var result = new List<string>();
            foreach (var item in lstSchema.Items)
            {
                result.Add(item.ToString());
            }
            _presenter.UpdateSchema(result);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            var result = new List<string>();
            foreach (var item in lstSchema.SelectedItems)
            {
                result.Add(item.ToString());
            }
            _presenter.UpdateSchema(result);
        }


        private void linkLabel5_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            _presenter.RefreshCurrentDatabases();
        }


        public void RefreshDatabaseList(List<string> databaseList)
        {
            lstExistDatabases.DataSource = databaseList;
        }

        private void btnBackupAll_Click(object sender, EventArgs e)
        {
            var result = new List<string>();
            foreach (var item in lstExistDatabases.Items)
            {
                result.Add(item.ToString());
            }
            _presenter.BackupDatabase(result);
        }

        private void btnBackup_Click(object sender, EventArgs e)
        {
            var result = new List<string>();
            foreach (var item in lstExistDatabases.SelectedItems)
            {
                result.Add(item.ToString());
            }
            _presenter.BackupDatabase(result);
        }

        private void btnOpenFolder_Click(object sender, EventArgs e)
        {
            Process.Start("explorer.exe", Application.StartupPath); 
        }

        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            _presenter.RefreshBackups();
        }






        public void RefreshBackupList(List<string> result)
        {
            lstBackups.DataSource = result;
        }

        private void btnBackupToInit_Click(object sender, EventArgs e)
        {
            var result = new List<string>();
            foreach (var item in lstExistDatabases.Items)
            {
                result.Add(item.ToString());
            }
            _presenter.BackupToInit(result);
        }

        private List<string> allBackups = new List<string>();

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (allBackups.Count == 0)
            {
                allBackups = (List<string>) (lstBackups.DataSource);
            }

            var checkBox = (CheckBox) sender;
            if(checkBox.Checked)
            {
                lstBackups.DataSource = allBackups.Where(p => p.StartsWith("INIT")).ToList();
            }else
            {
                lstBackups.DataSource = allBackups;
            }
        }

        private void btnRestore_Click(object sender, EventArgs e)
        {
            var result = new List<string>();
            foreach (var item in lstBackups.SelectedItems)
            {
                result.Add(item.ToString());
            }
            _presenter.RestoreFromBackups(result);
        }

        private void btnRestoreInit_Click(object sender, EventArgs e)
        {
            var result = new List<string>();
            foreach (var item in lstBackups.Items)
            {
                if (item.ToString().StartsWith("INIT"))
                {
                    result.Add(item.ToString());
                }
            }
            _presenter.RestoreFromBackups(result);
        }



        private string appFuncWebUrl = "http://localhost:2733";
        private void btnGoAppfunctionWeb_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate(appFuncWebUrl);
        }

        private void btnWebGoOut_Click(object sender, EventArgs e)
        {
            Form form = new Form();
            WebBrowser wb2 = new WebBrowser();
            wb2.Navigate(appFuncWebUrl);
            wb2.Dock = DockStyle.Fill;
            form.Controls.Add(wb2);
            form.Width = 1024;
            form.Height = 768;
            form.Show();
        }

    }
}
